package com.zbxx.practice.jdbcpool;

import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;

/**
 * 原生 jdbc 连接池
 * @author 万仁杰
 * @date 2019/05/27
 */

public class DataSourcePool implements DataSource {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(DataSourcePool.class);

    private static List<Connection> connectionPool =new CopyOnWriteArrayList<>();

    static {
        try {
            for (int i = 0;i < 10;i++){
                connectionPool.add(ConnectionConfig.getConnection());
            }
        } catch (SQLException e) {
            logger.error("获取连接失败：{}",e.getMessage());
        } catch (ClassNotFoundException e) {
            logger.error("获取连接失败：{}",e.getMessage());
        }
    }

    @Override
    public  Connection getConnection()  {
            if(getPoolSize()>0){
                   Connection connection = connectionPool.remove(0);
                   Connection proxyConnection = (Connection) Proxy.newProxyInstance(connection.getClass().getClassLoader(),connection.getClass().getInterfaces(),
                            ((proxy, method, args) ->{
                                if(!"close".equals(method.getName())){
                                    return method.invoke(connection,args);
                                }else{
                                    connectionPool.add(connection);
                                    return  null;
                                }
                            } ));
                    return proxyConnection;

                }
            Connection connection = null;
        try {
            connection =  ConnectionConfig.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }


    public  int getPoolSize(){
        return connectionPool.size();
    }

}
